package zxs.sino.elite.hub.service.app.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import com.github.yulichang.base.MPJBaseServiceImpl;
import zxs.sino.elite.hub.enums.user.InterestLikeType;
import zxs.sino.elite.hub.mapper.app.InterestMapper;
import zxs.sino.elite.hub.mapper.app.UserFollowsMapper;
import zxs.sino.elite.hub.model.app.UserFollowsEntity;
import zxs.sino.elite.hub.model.app.UserLevelEntity;
import zxs.sino.elite.hub.model.app.UserPointsEntity;
import zxs.sino.elite.hub.model.form.LoginForm;
import zxs.sino.elite.hub.model.vo.app.UserVo;
import zxs.sino.elite.hub.model.vo.sys.AppUserVo;
import zxs.sino.elite.hub.utils.PageUtil;

import zxs.sino.elite.hub.mapper.app.UserMapper;
import zxs.sino.elite.hub.model.app.UserEntity;
import zxs.sino.elite.hub.service.app.UserService;
import zxs.sino.elite.hub.utils.Query;
import zxs.sino.elite.hub.utils.UserContext;


@Service("userService")
@RequiredArgsConstructor
public class UserServiceImpl extends MPJBaseServiceImpl<UserMapper, UserEntity> implements UserService {

    private final InterestMapper interestMapper;

    private final UserFollowsMapper userFollowsMapper;

    /**
     * 分页查询用户列表
     *
     * @param params
     * @return
     */
    @Override
    public PageUtil queryPage(Map<String, Object> params) {
        MPJLambdaWrapper<UserEntity> wrapper = new MPJLambdaWrapper<>();

        if (StringUtils.isNotBlank((String) params.get("key"))) {
            wrapper.eq(UserEntity::getNickname, params.get("key"));
        }

        wrapper.selectAs(UserEntity::getUserId, AppUserVo::getUserId)
                .selectAs(UserEntity::getNickname, AppUserVo::getNickname)
                .selectAs(UserEntity::getAvatarUrl, AppUserVo::getAvatarUrl)
                .selectAs(UserEntity::getGender, AppUserVo::getGender)
                .selectAs(UserEntity::getBirthday, AppUserVo::getBirth)
                .selectAs(UserEntity::getPersonalProfile, AppUserVo::getPersonalProfile)
                .selectAs(UserEntity::getArea, AppUserVo::getCity)
                .selectAs(UserEntity::getStatus, AppUserVo::getStatus)
                .selectAs(UserLevelEntity::getLevelName, AppUserVo::getLevelName)
                .selectAs(UserPointsEntity::getCurrentPoints, AppUserVo::getCurrentPoints)
                .selectAs(UserEntity::getCreateType, AppUserVo::getCreateType)
                .selectAs(UserEntity::getCreateTime, AppUserVo::getCreateTime)
                .selectAs(UserEntity::getUpdateTime, AppUserVo::getUpdateTime)
                .leftJoin(UserPointsEntity.class, UserPointsEntity::getUserId, UserEntity::getUserId)
                .leftJoin(UserLevelEntity.class, on ->
                        on.ge(UserPointsEntity::getCurrentPoints, UserLevelEntity::getMinPoints)
                                .le(UserPointsEntity::getCurrentPoints, UserLevelEntity::getMaxPoints)
                );

        IPage<AppUserVo> page = this.selectJoinListPage(new Query<AppUserVo>().getPage(params), AppUserVo.class, wrapper);
        return new PageUtil(page);
    }

    /**
     * 更新用户状态
     *
     * @param loginForm
     */
    @Override
    public void updateStatus(LoginForm loginForm) {
        this.update(new LambdaUpdateWrapper<UserEntity>()
                .set(UserEntity::getStatus, loginForm.getStatus())
                .eq(UserEntity::getUserId, loginForm.getUserId())
        );
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    @Override
    public UserVo getUserInfo(Long userId) {
        if (userId == null) {
            userId = UserContext.getUserId();
        }

        // 获取用户的基本信息
        UserVo userInfo = this.baseMapper.getUserInfo(userId);

        // 获取用户的感兴趣名称
        List<String> interestName = interestMapper.getInterestName(userId, InterestLikeType.LIKE.getCode());
        userInfo.setIsInterestedName(interestName);

        // 获取用户的不感兴趣名称
        List<String> unInterestName = interestMapper.getInterestName(userId, InterestLikeType.DISLIKE.getCode());
        userInfo.setIsNotInterestedName(unInterestName);

        // 判断需要查询的用户编号是否为当前登录的用户
        // 是 不做处理判断
        // 不是 查询当前登录的用户是否关注了查询用户
        if (userId.equals(UserContext.getUserId())) {
            Long count = userFollowsMapper.selectCount(
                    new LambdaQueryWrapper<UserFollowsEntity>()
                            .eq(UserFollowsEntity::getFollowedId, userId)
            );
            if (count > 0) {
               userInfo.setFollowed(true);
            }
        }

        return userInfo;
    }

    /**
     * 获取优质创作者(2个)
     *
     * @return
     */
    @Override
    public List<UserVo> getPremiumActiveCreators() {
        // 1.获取最近发表时间比较多的用户信息
        this.baseMapper.getPremiumActiveCreators();

        return Collections.emptyList();
    }

}